package cn.yunhe.daoImpl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.yunhe.dao.AdminDao;
import cn.yunhe.factory.SqlSessionFactoryUtil;
import cn.yunhe.model.Admin;

public class AdminDaoImpl implements AdminDao {

	public List<Admin> searchAllAdmin() {
		SqlSession sqlSession = null;
		List<Admin> listAdmin = null;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			listAdmin = sqlSession.selectList("adminMapper.searchAllAdmin");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return listAdmin;
	}

	public boolean addAdmin(Admin admin) {
		SqlSession sqlSession = null;
		int result = 0;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			result = sqlSession.insert("adminMapper.addAdmin", admin);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return result > 0 ? true : false;
	}

	public boolean deleteAdmin(Admin admin) {
		SqlSession sqlSession = null;
		int result = 0;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			result = sqlSession.delete("adminMapper.deleteAdmin", admin);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return result > 0 ? true : false;
	}

	public Admin loginAdmin(Admin admin) {
		SqlSession sqlSession = null;
		Admin ad = null;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			ad = sqlSession.selectOne("adminMapper.loginAdmin", admin);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return ad;
	}

	public boolean updateAdmin(Admin admin) {
		SqlSession sqlSession = null;
		int result = 0;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			result = sqlSession.update("adminMapper.updateAdmin", admin);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return result > 0 ? true : false;
	}

	@Test
	public void test(){
		SqlSession sqlSession = null;
		Admin admin = new Admin(1,"kgy","kgy"); 
		Admin ad = null;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			ad = sqlSession.selectOne("adminMapper.testAdmin", admin);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
	}
	@Test
	public void test2(){
		SqlSession sqlSession = null;
		Admin admin = new Admin(1,"%"+"kgy"+"%","kgy"); 
		Admin ad = null;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			ad = sqlSession.selectOne("adminMapper.test2Admin", admin);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
	}
}
